package eclipse.client.forms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import eclipse.*;
import eclipse.client.*;
import eclipse.lib.sockets.*;
/**
 * The MouseHandler class handles all mouse functions for any menu. 
 * This class is not intended to be used outside of the Application.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class MouseHandler implements MouseListener
{
	private final int MENU_STATE;
	/**	Launches the MouseHandler with a specific listening menu. */
	public MouseHandler(int iMenu_State)
	{
		MENU_STATE = iMenu_State;
	}
	/** Launches the MouseHandler with a specific listening menu and adds it */
	public MouseHandler(int iMenu_State, Container dstSource)
	{
		dstSource.addMouseListener(this);
		MENU_STATE = iMenu_State;
	}
	/** Launches the MouseHandler with a specific listening menu and adds it */
	public MouseHandler(Container dstSource, int iMenu_State)
	{
		dstSource.addMouseListener(this);
		MENU_STATE = iMenu_State;
	}
	public void mouseClicked(MouseEvent e)
	{	
		if(MENU_STATE == Constants.MENUSTATE_MAINMENU)
			MainMenuMouseEvents(e);
		else if(MENU_STATE == Constants.MENUSTATE_LOGINMENU)
			LoginMenuMouseEvents(e);
		else if(MENU_STATE == Constants.MENUSTATE_NEWACCOUNTMENU)
			NewAccountMenuMouseEvents(e);
	}
	public final void mouseEntered(MouseEvent e) {}
	public final void mouseExited(MouseEvent e) {}
	public final void mousePressed(MouseEvent e) {}
	public final void mouseReleased(MouseEvent e) {}
	private final void LoginMenuMouseEvents(MouseEvent e)
	{
		final int x = e.getX();
		final int y = e.getY();
		if(x > Database.LOGINEXITX &&
			x < Database.LOGINEXITW + Database.LOGINEXITX &&
			y > Database.LOGINEXITY &&
			y < Database.LOGINEXITH + Database.LOGINEXITY
		)
		{				 
			Database.Login.setVisible(false);
			Database.MainMenu.setVisible(true);
			return;
		}		
		if(x > Database.LOGINLOGINX &&
			x < Database.LOGINLOGINX + Database.LOGINLOGINW &&
			y > Database.LOGINLOGINY &&
			y < Database.LOGINLOGINY + Database.LOGINLOGINH
		)
		{
			if(Database.AllDataReceived)
			{
				Database.USERNAME = Database.Login.txtUName.getText();
				Database.PASSWORD = new String(Database.Login.txtPWord.getPassword());				
				if(Database.USERNAME.length() < 6)
				{
					JOptionPane.showMessageDialog(null, "Your username must be at least three characters in length.", "Login Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if(Database.PASSWORD.length() < 6)
				{
					JOptionPane.showMessageDialog(null, "Your password must be at least three characters in length.", "Login Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				ClientDataHandle.requestLogin();				
			}
			else
				JOptionPane.showMessageDialog(null, "Please try again, attempting to retrieve all data from the server...");
			return;
		}
		System.out.println("X: " + x + "Y: " + y);
	}
	private final void MainMenuMouseEvents(MouseEvent e)
	{
		final int x = e.getX();
		final int y = e.getY();
		if(x > Database.MAINMENUEXITX &&
			x < Database.MAINMENUEXITW + Database.MAINMENUEXITX && 
			y > Database.MAINMENUEXITY &&
			y < Database.MAINMENUEXITH + Database.MAINMENUEXITY)
		{	
			System.exit(0);
			return;
		}
		if(x > Database.MAINMENULOGINX &&
			x < Database.MAINMENULOGINW + Database.MAINMENULOGINX &&
			y > Database.MAINMENULOGINY &&
			y < Database.MAINMENULOGINH + Database.MAINMENULOGINY
		)
		{
			Database.Login.setVisible(true);
			Database.MainMenu.setVisible(false);
			return;
		}
		if(x > Database.MAINMENUNEWACCOUNTX &&
			x < Database.MAINMENUNEWACCOUNTW + Database.MAINMENUNEWACCOUNTX &&
			y > Database.MAINMENUNEWACCOUNTY &&
			y < Database.MAINMENUNEWACCOUNTY + Database.MAINMENUNEWACCOUNTH
		)	
		{
			Database.NewAccount.setVisible(true);
			Database.MainMenu.setVisible(false);
			return;
		}
		System.out.println("X: " + x + "Y: " + y);
	}
	private final void NewAccountMenuMouseEvents(MouseEvent e)
	{
		final int x = e.getX();
		final int y = e.getY();
		final String password, confword, username;				
		
		if(x > Database.NEWACCOUNTCONFX &&
			x < Database.NEWACCOUNTCONFW + Database.NEWACCOUNTCONFX &&
			y > Database.NEWACCOUNTCONFY &&
			y < Database.NEWACCOUNTCONFH + Database.NEWACCOUNTCONFY
		)
		{
			password = new String(Database.NewAccount.txtPWord.getPassword());
			confword = new String(Database.NewAccount.txtCWord.getPassword());
			username = Database.NewAccount.txtUName.getText();
			if(!password.equals(confword))
			{
				JOptionPane.showMessageDialog(null, "Please confirm that you passwords match!");
				return;
			}
			if(password.length() < 5)
			{
				JOptionPane.showMessageDialog(null, "Your password has to have a minimum of 5 characters!");
				return;
			}
			if(username.length() < 3)
			{
				JOptionPane.showMessageDialog(null, "Your username has to have a minimum of 3 characters!");
				return;
			}
			Database.USERNAME = username;
			Database.PASSWORD = password;
			eclipse.lib.sockets.ClientDataHandle.requestNewAccount();
			return;
		}
		if(x > Database.NEWACCOUNTEXITX &&
			x < Database.NEWACCOUNTEXITW + Database.NEWACCOUNTEXITX &&
			y > Database.NEWACCOUNTEXITY &&
			y < Database.NEWACCOUNTEXITH + Database.NEWACCOUNTEXITY
		)
		{
			Database.NewAccount.setVisible(false);
			Database.MainMenu.setVisible(true);
			return;
		}
			
		System.out.println("X: " + x + "Y: " + y);
	}
}
